import { MigrationInterface, QueryRunner } from "typeorm";

export class CrateUser1703732998741 implements MigrationInterface {
    name = 'CrateUser1703732998741'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            CREATE TABLE \`user\` (
                \`id\` int NOT NULL AUTO_INCREMENT,
                \`email\` varchar(100) NULL,
                \`password\` varchar(255) NOT NULL,
                \`username\` varchar(20) NOT NULL COMMENT '用户名, 用于登录',
                \`nickname\` varchar(100) NULL COMMENT '昵称',
                \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
                UNIQUE INDEX \`idx_email\` (\`email\`),
                PRIMARY KEY (\`id\`)
            ) ENGINE = InnoDB
        `);
        await queryRunner.query(`
            CREATE TABLE \`role\` (
                \`id\` int NOT NULL AUTO_INCREMENT,
                \`name\` varchar(50) NOT NULL COMMENT '角色名',
                \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
                PRIMARY KEY (\`id\`)
            ) ENGINE = InnoDB
        `);
        await queryRunner.query(`
            CREATE TABLE \`menu\` (
                \`id\` int NOT NULL AUTO_INCREMENT,
                \`name\` varchar(50) NOT NULL COMMENT '菜单/API名称',
                \`type\` tinyint NOT NULL COMMENT '类型[1:菜单, 2:API]' DEFAULT '1',
                \`path\` varchar(100) NULL COMMENT '路径',
                \`icon\` varchar(50) NULL COMMENT '图标',
                \`order\` int NOT NULL COMMENT '排序' DEFAULT '0',
                \`parent_id\` int NULL COMMENT '父级id',
                \`is_show\` tinyint NOT NULL COMMENT '是否显示, 1 - 显示; 0 - 隐藏' DEFAULT '1',
                \`component\` varchar(100) NULL COMMENT '前端组件路径',
                \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
                PRIMARY KEY (\`id\`)
            ) ENGINE = InnoDB
        `);
        await queryRunner.query(`
            CREATE TABLE \`user_role\` (
                \`user_id\` int NOT NULL COMMENT '用户id',
                \`role_id\` int NOT NULL COMMENT '角色id',
                \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
                PRIMARY KEY (\`user_id\`, \`role_id\`)
            ) ENGINE = InnoDB
        `);
        await queryRunner.query(`
            CREATE TABLE \`role_menu\` (
                \`role_id\` int NOT NULL COMMENT '角色id',
                \`menu_id\` int NOT NULL COMMENT '菜单id',
                \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
                PRIMARY KEY (\`role_id\`, \`menu_id\`)
            ) ENGINE = InnoDB
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            DROP TABLE \`role_menu\`
        `);
        await queryRunner.query(`
            DROP TABLE \`user_role\`
        `);
        await queryRunner.query(`
            DROP TABLE \`menu\`
        `);
        await queryRunner.query(`
            DROP TABLE \`role\`
        `);
        await queryRunner.query(`
            DROP INDEX \`idx_email\` ON \`user\`
        `);
        await queryRunner.query(`
            DROP TABLE \`user\`
        `);
    }

}
